package net.sppan.blog.utils;

import java.util.HashMap;
import java.util.Map;

public enum OfferType {

	cpc(1),

	cpm(2),

	cpi(3),

	cpa(4);

	private static Map<Integer, String> codeMap;
	private static Map<String, Integer> nameMap;
	private Integer code;

	private OfferType() {

	}

	private OfferType(Integer code) {
		setCode(code);
	}

	/**
	 * @param code
	 *            the code to set
	 */
	private void setCode(Integer code) {
		this.code = code;
		if (codeMap == null) {
			codeMap = new HashMap<>();
		}
		if (nameMap == null) {
			nameMap = new HashMap<>();
		}
		nameMap.put(name(), code);
		codeMap.put(code, name());
	}

	/**
	 * 根据name 获取code
	 * 
	 * @param name
	 * @return
	 */
	public static Integer getCode(String name) {
		Integer code = nameMap.get(name);
		if (code == null) {
			code = -1;
		}
		return code;
	}

	/**
	 * 根据code 获取name
	 * 
	 * @param code
	 * @return
	 */
	public static String getName(Integer code) {
		String name = codeMap.get(code);
		if (name == null) {
			name = "unknow";
		}
		return name;
	}

	public String getCode() {
		return this.code.toString();
	}

	public boolean equals(String code) {
		if (this.name().equals(code)) {
			return true;
		}
		return false;
	}

}
